package com.start.pay.offLine;

import com.start.common.exceptions.StartBusException;
import com.start.pay.core.entity.StartPayOrderEntity;
import com.start.pay.core.entity.StartPlatformPlayConfig;
import com.start.pay.core.enums.StartPayState;
import com.start.pay.core.interfaces.StartBasePayPlatform;
import com.start.pay.core.model.PlatformOrderPayResult;
import com.start.pay.core.model.PlatformOrderResult;
import com.start.pay.core.model.PlatformResult;
import com.start.pay.core.service.StartPayOrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@Component
public class StartPayPlatformOffLine implements StartBasePayPlatform<StartPlatformPlayConfig, StartPayOrderService> {

    public static final String TYPE = "offLinePay";

    private static final Map<String, StartPayOrderService> serviceMap = new HashMap<>();
    private static final Map<String, StartPlatformPlayConfig> platformPlayConfigMap = new HashMap<>();

    @Autowired
    private StartPayOrderService startPayOrderService;

    @Override
    public String getPlatformType() {
        return TYPE;
    }

    @Override
    public String getPlatformTypeLabel() {
        return "线下支付";
    }

    @Override
    public boolean hasAppId(String appId) {
        return serviceMap.containsKey(appId);
    }

    @Override
    public boolean addPlayConfig(StartPlatformPlayConfig config) {
        if (serviceMap.containsKey(config.getAppId())) {
            return true;
        }
        serviceMap.put(config.getAppId(), startPayOrderService);
        platformPlayConfigMap.put(config.getAppId(), config);
        return true;
    }

    @Override
    public boolean removeByAppId(String appId) {
        if (!serviceMap.containsKey(appId)) {
            return true;
        }
        serviceMap.remove(appId);
        platformPlayConfigMap.remove(appId);
        return true;
    }

    @Override
    public StartPayOrderService getPlatformServiceByAppId(String appId) {
        if (!serviceMap.containsKey(appId)) {
            return null;
        }
        return serviceMap.get(appId);
    }

    @Override
    public boolean updateByAppId(StartPlatformPlayConfig config) {
        if (!serviceMap.containsKey(config.getAppId())) {
            return false;
        }
        removeByAppId(config.getAppId());
        return addPlayConfig(config);
    }

    @Override
    public List<StartPlatformPlayConfig> allConfig() {
        if (platformPlayConfigMap.isEmpty()) {
            return new ArrayList<>();
        }
        return new ArrayList<>(platformPlayConfigMap.values());
    }

    @Override
    public Class<StartPlatformPlayConfig> getConfigClazz() {
        return StartPlatformPlayConfig.class;
    }

    @Override
    public PlatformResult createPay(StartPayOrderEntity entity) {
        if (!hasAppId(entity.getAppId())) {
            log.error("未找到AppId为:" + entity.getAppId() + "的支付配置");
            throw new RuntimeException("未找到AppId为:" + entity.getAppId() + "的支付配置");
        }
        PlatformResult result = new PlatformOrderResult();
        result.setSuccess(true);
        result.setResultData(entity);
        result.setPayState(StartPayState.PAY_SUCCESS);
        return result;
    }

    @Override
    public PlatformOrderResult payCallBack(String appId, String platformCallData) {
        throw new StartBusException("线下支付不支持平台数据回调");
    }

    @Override
    public PlatformResult refundOrder(StartPayOrderEntity entity) {
        PlatformResult result = new PlatformOrderResult();
        result.setSuccess(true);
        result.setResultData(entity);
        result.setPayState(StartPayState.PAY_REFUND_SUCCESS);
        return result;
    }

    @Override
    public PlatformOrderResult refundCallBack(String appId, String platformCallData) {
        throw new StartBusException("线下支付不支持平台数据回调");
    }

    @Override
    public PlatformOrderPayResult queryPayOrder(StartPayOrderEntity orderEntity) {
        PlatformOrderPayResult result = new PlatformOrderPayResult();
        result.setQueryState(true);
        result.setPayState(orderEntity.getPayState());
        return result;
    }

    @Override
    public PlatformOrderPayResult queryRefundOrder(StartPayOrderEntity orderEntity) {
        PlatformOrderPayResult result = new PlatformOrderPayResult();
        result.setQueryState(true);
        result.setPayState(orderEntity.getPayState());
        return result;
    }
}
